/*
 * ProgramSettingsDialog.java
 *
 * Created on 13.5.2010, 11:28:40
 */
package net.parostroj.timetable.gui.dialogs;

import net.parostroj.timetable.gui.ApplicationModel;
import net.parostroj.timetable.gui.ProgramSettings;
import net.parostroj.timetable.gui.actions.execution.ActionUtils;
import net.parostroj.timetable.model.units.LengthUnit;
import net.parostroj.timetable.utils.ResourceLoader;

/**
 * Dialog for program settings.
 *
 * @author jub
 */
public class ProgramSettingsDialog extends javax.swing.JDialog {

    private ApplicationModel model;

    /** Creates new form ProgramSettingsDialog */
    public ProgramSettingsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        for (LengthUnit unit : LengthUnit.values()) {
            if (unit.isScaleDependent())
                unitComboBox.addItem(unit);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        javax.swing.JPanel dataPanel = new javax.swing.JPanel();
        javax.swing.JLabel nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        warningAutoECCorrectionCheckBox = new javax.swing.JCheckBox();
        javax.swing.JLabel unitLabel = new javax.swing.JLabel();
        unitComboBox = new javax.swing.JComboBox();
        javax.swing.JPanel buttonPanel = new javax.swing.JPanel();
        javax.swing.JButton okButton = new javax.swing.JButton();
        javax.swing.JButton cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(ResourceLoader.getString("program.settings.title")); // NOI18N
        setResizable(false);

        dataPanel.setLayout(new java.awt.GridBagLayout());

        nameLabel.setText(ResourceLoader.getString("program.settings.username")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 4);
        dataPanel.add(nameLabel, gridBagConstraints);

        nameTextField.setColumns(25);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        dataPanel.add(nameTextField, gridBagConstraints);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gt_texts"); // NOI18N
        warningAutoECCorrectionCheckBox.setText(bundle.getString("program.settings.speedautochange.warning")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        dataPanel.add(warningAutoECCorrectionCheckBox, gridBagConstraints);

        unitLabel.setText(bundle.getString("program.settings.unit")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        dataPanel.add(unitLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        dataPanel.add(unitComboBox, gridBagConstraints);

        getContentPane().add(dataPanel, java.awt.BorderLayout.CENTER);

        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        okButton.setText(ResourceLoader.getString("button.ok")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        cancelButton.setText(ResourceLoader.getString("button.cancel")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(cancelButton);

        getContentPane().add(buttonPanel, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (writeBackValues())
            this.setVisible(false);
        else
            ActionUtils.showError("Error", this);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    public void showDialog(ApplicationModel model) {
        // set model
        this.setModel(model);
        // show dialog
        this.setVisible(true);
    }

    private void setModel(ApplicationModel model) {
        this.model = model;
        this.updateValues();
    }

    private void updateValues() {
        this.nameTextField.setText(model.getProgramSettings().getUserNameOrSystemUser());
        this.warningAutoECCorrectionCheckBox.setSelected(model.getProgramSettings().isWarningAutoECCorrection());
        this.unitComboBox.setSelectedItem(model.getProgramSettings().getLengthUnit());
    }

    private boolean writeBackValues() {
        ProgramSettings ps = model.getProgramSettings();
        String name = nameTextField.getText().trim();
        if (name.equals(ps.getSystemUser()) || "".equals(name))
            ps.setUserName(null);
        else
            ps.setUserName(name);
        ps.setWarningAutoECCorrection(warningAutoECCorrectionCheckBox.isSelected());
        ps.setLengthUnit((LengthUnit) unitComboBox.getSelectedItem());
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField nameTextField;
    private javax.swing.JComboBox unitComboBox;
    private javax.swing.JCheckBox warningAutoECCorrectionCheckBox;
    // End of variables declaration//GEN-END:variables
}
